﻿using Caliburn.Micro;
using myapp.Databases.DAO;
using myapp.Events;
using myapp.Events.ViewModel;
using myapp.Instances;
using myapp.ViewModels;
using myappcore.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myapp.ViewModels
{
    /// <summary>
    /// Detail of Possition Group
    /// </summary>
    public class PossitionListViewModel : IBaseContentViewModel, IHandle<ViewModelPossitionGroupSelectedEvent>
    {
        #region Attribute
        private IObservableCollection<PossitionViewModel> _possitionViewList;

        private static PossitionListViewModel _instance;
        #endregion

        #region Properties

        /// <summary>
        /// Group of possition
        /// </summary>
        public PossitionGroup PossitionGroup { get; set; }

        /// <summary>
        /// List Possition of Group
        /// </summary>
        public IObservableCollection<PossitionViewModel> PossitionViewList
        {
            get
            {
                if (null == _possitionViewList)
                {
                    _possitionViewList = new BindableCollection<PossitionViewModel>();
                }
                if (IsChanged)
                {
                    _possitionViewList.Clear();
                    if (null != PossitionGroup)
                    {
                        List<Possition> lst = PossitionDao.ListPossitionOfGroup(PossitionGroup);
                        foreach (Possition pos in lst)
                        {
                            PossitionViewModel posView = new PossitionViewModel()
                            {
                                CurrentColor = MainInstance.Instance.ColorConfig.PosstionNormal,
                                Possition = pos
                            };
                            _possitionViewList.Add(posView);
                        }
                    }
                    if (_possitionViewList.Count > 0)
                    {
                        IsChanged = false;
                    }
                }
                return _possitionViewList;
            }
        }

        public static PossitionListViewModel Instance
        {
            get
            {
                if (null == _instance)
                {
                    _instance = new PossitionListViewModel();
                    _instance.IsChanged = true;
                }
                return _instance;
            }
        }
        #endregion
        public PossitionListViewModel()
        {
            MainInstance.Instance.EventAggregator.Subscribe(this);
            DisplayName = "Danh Sách Chỗ Ngồi";
            CurrentColor = MainInstance.Instance.ColorConfig.PosstionNormal;
        }
        #region Constructure


        #endregion

        #region Method

        /// <summary>
        /// Process handle PossitionGroupSelectedEvent
        /// </summary>
        /// <param name="message"></param>
        public void Handle(ViewModelPossitionGroupSelectedEvent message)
        {
            if (null != message && null != message.PossitionGroupViewModel)
            {
                if (PossitionGroup != message.PossitionGroupViewModel.PossitionGroup)
                {
                    IsChanged = true;
                    PossitionGroup = message.PossitionGroupViewModel.PossitionGroup;
                    NotifyOfPropertyChange(() => PossitionViewList);
                }
            }
        }
        #endregion
    }
}
